<?php

namespace app\service;

//用户饰品服务
use app\model\Ornament;
use app\model\UserOrnament;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class UserOrnamentsService extends BaseService
{


    //添加玩家饰品
    public function add_user_ornament($user_id, $ornament_id, $num): array
    {
        //查询卡牌是否存在
        $ornament = Ornament::where("id", $ornament_id)->find();
        if (!$ornament) {
            return $this->returnFail("卡牌不存在");
        }

        //查询玩家是否已经拥有该饰品
        $user_ornament = UserOrnament::where("user_id", $user_id)->where("ornament_id", $ornament_id)->find();
        if (!$user_ornament) {
            $user_ornament = new UserOrnament();
            $user_ornament->user_id = $user_id;
            $user_ornament->ornament_id = $ornament_id;
        }
        $user_ornament->num = $user_ornament->num + $num;
        $user_ornament->save();

        return $this->returnSuccess("添加成功");
    }



    /**
     * 获取建筑饰品列表
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function list($user_id): array
    {
        $ornaments_list = (new Ornament)->select()->toArray();

        //判断用户是否拥有饰品
        $user_ornaments = (new UserOrnament())->where('user_id', $user_id)->select()->toArray();

        //提取用户拥有的饰品id
        $user_ornaments_ids = array_column($user_ornaments, 'ornament_id');

        foreach ($ornaments_list as &$ornament) {
            if (in_array($ornament['id'], $user_ornaments_ids)) {
                $ornament['own'] = 1;
            } else {
                $ornament['own'] = 0;
            }

        }

        return $this->returnSuccess($ornaments_list);
    }



    //购买饰品
    public function buy($user_id, $ornament_id): array
    {
        $user_ornament = new UserOrnament();
        $user_ornament->user_id = $user_id;
        $user_ornament->ornament_id = $ornament_id;
        $user_ornament->save();

        return $this->returnSuccess();
    }


    /**
     * 安装饰品
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function install($user_id, $user_ornament_id, $position): array
    {
        $user_ornament = (new UserOrnament())->where('user_id', $user_id)->where('id', $user_ornament_id)->find();
        if (!$user_ornament) {
            return $this->returnFail('用户没有该饰品');
        }
        $user_ornament->position = $position;
        $user_ornament->state = 1;
        $user_ornament->save();
        return $this->returnSuccess();
    }

    /**
     * 卸载饰品
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function uninstall($user_id, $user_ornament_id): array
    {
        $user_ornament = (new UserOrnament())->where('user_id', $user_id)->where('id', $user_ornament_id)->find();
        if (!$user_ornament) {
            return $this->returnFail('用户没有该饰品');
        }
        $user_ornament->position = '';
        $user_ornament->state = 0;
        $user_ornament->save();
        return $this->returnSuccess();
    }





}